<?php

namespace app\console\command;

use app\common\model\Vod;
use Aws\AwsService;
use CloudFlare\CloudFlareApiClient;
use HuaweiVod\HuaweiVod;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Log;

class UpdateCloudflareVodStatus extends Command
{
    protected function configure()
    {
        $this->setName('update_cloudflare_vod_status')
            ->setDescription('update cloudflare vod status');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->info("execute update cloudflare vod status");
        try {
            $vodList = Db::name('vod')
                ->where('video_type', '=', 'M3U8')
                ->where('platform', '=', 'cloudflare')
                ->where('status', '<', Vod::VOD_STATUS_PUBLISHED)
                ->select();
            $cfClient = new CloudFlareApiClient();
            foreach ($vodList as $vod) {
                if ($vod['status'] == Vod::VOD_STATUS_NO_UPLOAD) {
                    $result = $cfClient->getVideoDetail($vod['asset_id']);
                    if ($result) {
                        if (is_string($result)) {
                            $result = json_decode($result, true);
                        }
                        Log::info('result:' . json_encode($result));
                        if ($result['success'] == false) {
                            Log::error('get video status failed');
                            continue;
                        }
                        if ($result['result']['status']['state'] == 'ready') {
                            $vod['status'] = Vod::VOD_STATUS_UPLOADED;
                        }
                    }
                }
                if ($vod['status'] == Vod::VOD_STATUS_UPLOADED) {
                    // 已上传完成,开启mp4下载
                    $result = $cfClient->postVideoDownloads($vod['asset_id']);
                    if ($result) {
                        if (is_string($result)) {
                            $result = json_decode($result, true);
                        }
                        Log::info('result:' . json_encode($result));
                        if ($result['success'] == true) {
                            if (!empty($result['result']['default']) && $result['result']['default']['status'] == 'ready') {
                                $vod['status'] = Vod::VOD_STATUS_PUBLISHED;
                                $vod['url'] = $result['result']['default']['url'];
                                $vod['video_type'] = 'MP4';
                            } else {
                                Log::info('vod ' . $vod['id'] . ' is not ready to download,percent:' . $result['result']['default']['percentComplete']);
                            }
                        } else {
                            Log::error('get video downloads failed');
                        }
                    }
                }
                Vod::updateVod($vod['id'], $vod);
            }
        } catch (\Exception $exception) {
            Log::error($exception->getMessage());
            Log::error($exception->getTraceAsString());
            $output->error($exception->getMessage());
        }
    }
}
